<?php
namespace App\Api\Models;

use Illuminate\Database\Eloquent\Model;
use App\Api\Repositories\CategoryRepositorie;

class GoodsCategory extends Model
{

    protected $table = "cmc_goods_category";

    /**
     * 关联模型
     */
    public function Goods()
    {
        return $this->belongsTo(Goods::class,'cate_id','category_id');
    }

    public static function getCategoryData()
    {
        $all = self::get()->toArray();

        return CategoryRepositorie::getTreeList($all);
    }

    /**
     * 查询顶级分类
     * @return mixed
     */
    public static function getCategoryName($allowField)
    {
        return self::select($allowField)->where('pid',0)->get()->toArray();
    }

    /**
     * 查询子级数据
     * @param
     * @return mixed
     */
    public static function getGoodsValue($categoryId)
    {
        $allowFiled = [
            'goods_id',
            'goods_name',
            'price',
            'goods_image',
            'cate_id',
            'goods_state'
        ];

        return Goods::select($allowFiled)->where('cate_id',$categoryId)->where('goods_state',1)->get()->toArray();
    }
}
